<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap-responsive.css" />
    <link rel="stylesheet" type="text/css" href="../../Css/style.css" />
    <script type="text/javascript" src="../../Js/jquery.js"></script>
    <script type="text/javascript" src="../../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../../Js/ckform.js"></script>
    <script type="text/javascript" src="../../Js/common.js"></script>

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }
        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
    <script>
        String.prototype.GetValue= function(para) {
            let reg = new RegExp("(^|&)"+ para +"=([^&]*)(&|$)");
            let r = this.substr(this.indexOf("\?")+1).match(reg);
            if (r!=null) return unescape(r[2]); return null;
        }

        function validatarname() {
            var name = $("#rName").val();
            var msg = $("span[id='rNameMsg']");
            if(name == "") {
                msg.html("角色不能为空").css("color", "red");
                return false;
            } else {
                msg.html("ok").css("color", "green");
                return true;
            }
        }

        function validataState() {
            var all = $("input[name='status']");
            for(var i = 0; i < all.length; i++) {
                if($(all[i]).prop("checked")) {
                    return true;
                }
            }
            alert("状态为必选项");
            return false;
        }

        function validataPermission() {
            var all = $("input[name='check']");
            for(var i = 0; i < all.length; i++) {
                if($(all[i]).prop("checked")) {
                    return true;
                }
            }
            alert("状态为必选项");
            return false;
        }

        $(function() {
            $("#rName").blur(function() {
                validatarname();
            });
        });

let rId=null;
        let roleList=null;
        let mIdList=[];
        $(function (){
            let url=window.location.toString();
            rId=url.GetValue("rId");
           $.getJSON("/role/getinfo.do",{rId:rId},function (vo){
               let yeslist=vo.list2;
               let nolist=vo.list3;
               roleList=vo.list4;
               let role=vo.role;
               let rName=role.rName;
               let rState=role.rState;
               let str="";
               for (let i=0;i<yeslist.length;i++){
                   str+="<ul><label class='checkbox inline'><input type='checkbox' name='check' checked value=\""+yeslist[i].mId+"\" />"+yeslist[i].mName+"</label></ul>";
               }
               for (let i=0;i<nolist.length;i++){
                   str+="<ul><label class='checkbox inline'><input type='checkbox' name='check' value=\""+nolist[i].mId+"\" />"+nolist[i].mName+"</label></ul>";
               }
               if (rState==0){
                   $("input[name='status']:eq(0)").prop("checked",true);
               }else if (rState==1){
                   $("input[name='status']:eq(1)").prop("checked",true);
               }
               $("#info").html(str);
               $("#rName").val(rName);
           });


           $("#update").click(function (){
               if (validatarname()&&validataPermission()&&validataState()){
                   if (confirm("是否更新?")){
                       let rName=$("#rName").val();
                       let rState=$("input[name='status']:checked").val();
                       console.log(rState);
                       let mIds=$("input[name='check']:checked");
                       for(let i=0;i<mIds.length;i++){
                           mIdList.push($(mIds[i]).val());
                       }
                       let role={
                           rName:rName,
                           rState:rState,
                           rId:rId
                       }

                       $.getJSON("/role/updateroleobj.do",role,function (vo){
                       });

                       setTimeout(listctrl,1000);
                   }
               }
           });

        });

        function listctrl(){
            $.ajax({
                url: "/role/updaterolelist.do",
                type: "POST",
                data: {
                    "rId":rId,
                    "mIdList": mIdList,
                },
                traditional: true,//这里设置为true
                success: function(vo) {
                    if(vo.code==200){
                        alert("修改成功");
                        window.location.href="/pages/role/index.html";
                    }else {
                        alert(vo.msg);
                    }
                }
            });
        }
    </script>
</head>
<body>
<form action="index.html" method="post" class="definewidth m20" >
<table class="table table-bordered table-hover definewidth m10">
    <tr>
        <td width="10%" class="tableleft">角色名称</td>
        <td><input id="rName" type="text" name="title"/><span id="rNameMsg"></span></td>
    </tr>
    <tr>
        <td class="tableleft">状态</td>
        <td>
            <input type="radio" name="status" value="0"  /> 启用
           <input type="radio" name="status" value="1"  /> 禁用
        </td>
    </tr>
    <tr>
        <td class="tableleft">权限</td>
        <td id="info">

		</td>
    </tr>
    <tr>
        <td class="tableleft"></td>
        <td>
            <button id="update" type="button" class="btn btn-primary" type="button">更新</button> &nbsp;&nbsp;<button type="button" class="btn btn-success" name="backid" id="backid">返回列表</button>
        </td>
    </tr>
</table>
</form>
</body>
</html>
<script>
    $(function () {
        $(':checkbox[name="group[]"]').click(function () {
            $(':checkbox', $(this).closest('li')).prop('checked', this.checked);
        });

		$('#backid').click(function(){
				window.location.href="/pages/role/index.html";
		 });

    });
</script>